package com.quanyan.place.wrapper.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.entity.TbAdminUser;
import com.quanyan.place.entity.TbBizPlaceFinanceAudit;
import com.quanyan.place.entity.TbBizPlaceMemberOpHistory;
import com.quanyan.place.entity.TbBizPlaceOrder;
import com.quanyan.place.entity.vo.PlaceFinanceAuditDetailVo;
import com.quanyan.place.entity.vo.PlaceFinanceAuditVo;
import com.quanyan.place.entity.vo.ReqFinanceAudit;
import com.quanyan.place.entity.vo.ReqFinanceAuditSearch;
import com.quanyan.place.service.AdminUserService;
import com.quanyan.place.service.FinanceService;
import com.quanyan.place.wrapper.FinanceWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by yangyun on 2017/5/8.
 */
@Service("financeWrapper")
public class FinanceWrapperImpl implements FinanceWrapper {

    @Autowired
    FinanceService financeService;

    @Autowired
    AdminUserService adminUserService;

    /**
     * 更新初审结果
     * @param reqFinanceAudit
     */
    @Override
    public APIResponse executeFirstAudit(ReqFinanceAudit reqFinanceAudit, TbBizPlaceFinanceAudit tbBizPlaceFinanceAudit) {
        TbAdminUser adminUser = adminUserService.getAdminUser(reqFinanceAudit.getUid());
        if (PlaceFinanceAuditVo.AUDTI_RESULT_OK.equals(reqFinanceAudit.getResult())){
            // 审核通过
            tbBizPlaceFinanceAudit.setFirstAuditStatus(PlaceFinanceAuditVo.STATUS_AUDIT_PASS);
        } else {
            tbBizPlaceFinanceAudit.setFirstAuditStatus(PlaceFinanceAuditVo.STATUS_AUDIT_NOT_PASS);
        }
        tbBizPlaceFinanceAudit.setFirstAuditRemark(reqFinanceAudit.getAuditRemark());
        tbBizPlaceFinanceAudit.setFirstAuditTime(Calendar.getInstance().getTime());
        tbBizPlaceFinanceAudit.setFirstAuditUid(reqFinanceAudit.getUid());
        tbBizPlaceFinanceAudit.setFirstAuditUserName(adminUser.getTrueName());
        financeService.updateTbBizPlaceFinanceAudit(tbBizPlaceFinanceAudit);
        return APIResponse.returnSuccess();
    }

    /**
     * 获取审核列表
     * @param reqSearch
     * @return
     */
    @Override
    public PageObj<List<PlaceFinanceAuditVo>> getPlaceFinanceAuditList(ReqFinanceAuditSearch reqSearch) {
        List<PlaceFinanceAuditVo> result = new ArrayList<PlaceFinanceAuditVo>();
        int pageSize = Constants.DEFAULT_PAGE_SIZE;
        int pageNum = 0;  // 默认第一页
        if (null == reqSearch.getPageNum()){
            reqSearch.setPageNum(pageNum);
        }
        if (null == reqSearch.getPageSize()){
            reqSearch.setPageSize(pageSize);
        }
        int totalNum = financeService.countPlaceFinanceAudit(reqSearch);
        //List<TbBizPlaceFinanceAudit> records = financeService.getPlaceFinanceAuditList(reqSearch, pageSize, pageNum, totalNum);
        List<PlaceFinanceAuditVo> records = financeService.getPlaceFinanceAuditList(reqSearch);
        for (PlaceFinanceAuditVo record : records){
            if (PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_TWO.equals(record.getSubType())){
                // 固定场对公转账
                if (StringUtils.isEmpty(record.getExtendInfo2())){
                    continue;
                }
                Integer parentId = Integer.valueOf(record.getExtendInfo2());
                List<TbBizPlaceOrder> tbBizPlaceOrdersList = financeService.getOrdersByPrivateId(parentId);
                setPrivateOrdersInfo(record, tbBizPlaceOrdersList);
            }
            if (PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_ONE.equals(record.getSubType())||
                PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_TWO.equals(record.getSubType())||
                PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_THREE.equals(record.getSubType())||
                PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_FOUR.equals(record.getSubType())||
                PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_FIVE.equals(record.getSubType())){
                // 开单等
                TbBizPlaceOrder tbBizPlaceOrder = financeService.getOrdersByOrderNo(record.getBusinessNo());
                if (null != tbBizPlaceOrder){
                    record.setAmount(tbBizPlaceOrder.getDebit()); // TODO 实收
                }
            }
            if (PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_SIX.equals(record.getSubType())||
                PlaceFinanceAuditVo.FINANCE_AUDIT_SUBTYPE_SEVEN.equals(record.getSubType())){
                TbBizPlaceMemberOpHistory  opHistory= financeService.getMemberOpHistoryByOrderNo(record.getBusinessNo());
                if (opHistory!=null){
                    record.setAmount(opHistory.getAmount());
                    record.setCustomerMobile(opHistory.getMobile());
                    record.setCustomerName(opHistory.getUserName());
                }
            }
            switch (record.getSubType()){
                case 1:
                    record.setOrderType("开单");
                    record.setBusinessName("对公转账-订单付款");
                    break;
                case 2:
                    record.setOrderType("包场");
                    record.setBusinessName("对公转账-订单付款");
                    break;
                case 3:
                    record.setOrderType("门票");
                    record.setBusinessName("对公转账-订单付款");
                    break;
                case 4:
                    record.setOrderType("小商品");
                    record.setBusinessName("对公转账-订单付款");
                    break;
                case 5:
                    record.setOrderType("购物车");
                    record.setBusinessName("对公转账-订单付款");
                    break;
                case 6:
                    record.setOrderType("会员开卡");
                    record.setBusinessName("对公转账-会员开卡");
                    break;
                case 7:
                    record.setOrderType("会员充值");
                    record.setBusinessName("对公转账-会员充值");
                    break;
                case 8:
                    record.setOrderType("会员导入");
                    record.setBusinessName("会员导入-买断");
                    break;
                default:

            }
            record.setBusinessNo(record.getBusinessNo());
            record.setOrderNo(record.getBusinessNo()); // 订单号
            record.setMemberCardNo(record.getExtendInfo1()); // 会员卡号
        }
        return PageObj.create(totalNum, reqSearch.getPageNum(), reqSearch.getPageSize(), records);
    }

    private void setPrivateOrdersInfo(PlaceFinanceAuditVo record, List<TbBizPlaceOrder> tbBizPlaceOrdersList) {
        List<PlaceFinanceAuditDetailVo> detailVos = new ArrayList<PlaceFinanceAuditDetailVo>();
        for (TbBizPlaceOrder order : tbBizPlaceOrdersList){
            PlaceFinanceAuditDetailVo detailVo = new PlaceFinanceAuditDetailVo();
            detailVo.setObjectNo(order.getPlaceOrderId());
            detailVo.setObjectTime(order.getOrderTime()); // 消费日期
            detailVos.add(detailVo);
        }
        record.setFinanceAuditDetail(detailVos);
    }

    /**
     * 根据id获取审核记录
     * @param id
     * @return
     */
    @Override
    public TbBizPlaceFinanceAudit getPlaceFinanceAudit(Integer id) {
        return financeService.getPlaceFinanceAudit(id);
    }

    /**
     * 校验审核信息
     * @return
     */
    @Override
    public APIResponse validAuditInfo(ReqFinanceAudit reqFinanceAudit) {
        if (null == reqFinanceAudit){
            return APIResponse.returnFail("请求对象不能为空");
        }
        if (null == reqFinanceAudit.getId()){
            return APIResponse.returnFail("审核Id不能为空");
        }
        if (null == reqFinanceAudit.getUid()){
            return APIResponse.returnFail("审核人不能为空");
        }
        return APIResponse.returnSuccess();
    }
}
